﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECare.BUS.Data;
using ECare.BUS;
using ECare.Database;

namespace ECare.BUS
{
    public class FormulaHelper
    {

        public static int CreateFormula(FormulaInfo formula)
        {
            using (var context = new Entities())
            {
                var newformula = new formula();
                newformula.formula_code = formula.FormulaCode;
                newformula.formula_name = formula.FormulaName;
                newformula.formula_create_time = DateTime.Now;
                newformula.formula_isactive = formula.IsActive;
                newformula.formula_remark = formula.FormulaRemark;

                newformula.formula_create_by = ApplicationData.CurrentUserInfo.UserId;
                newformula.formula_create_time = DateTime.Now;
                newformula.formula_update_by = ApplicationData.CurrentUserInfo.RoleId;
                newformula.formula_update_time = DateTime.Now;

                context.formulae.AddObject(newformula);
                context.SaveChanges();
                formula.FormulaId = newformula.formulas_id;

                //create the details/.

                foreach (var detail in formula.Details)
                {
                    var newtail = new formula_detail();
                    newtail.resource_Id = detail.ResourceId;
                    newtail.formulas_id = formula.FormulaId;
                    newtail.formula_order = detail.Order;
                    newtail.formula_detail_number = detail.Number;
                    newtail.formula_detail_unit = detail.unit;

                    newtail.formula_detail_car = detail.CarNumber;
                    newtail.formula_detail_ischangecar = detail.Ischangecar;

                    context.formula_detail.AddObject(newtail);
                }
                context.SaveChanges();
                //        context.Database.ExecuteSqlCommand("delete from  formula_detail where formulas_id="+findres.f);
                return formula.FormulaId;
            }
        }
     
        public static bool UpdateFormula(FormulaInfo formula)
        {
            using (var context = new Entities())
            {
                var newformula = context.formulae.FirstOrDefault(a => a.formulas_id == formula.FormulaId);
                if (newformula == null)
                    return false;

                newformula.formula_code = formula.FormulaCode;
                newformula.formula_name = formula.FormulaName;
                newformula.formula_create_time = DateTime.Now;
                newformula.formula_isactive = formula.IsActive;
                newformula.formula_remark = formula.FormulaRemark;
           //     context.formulae.Add(newformula);

                newformula.formula_update_by = ApplicationData.CurrentUserInfo.UserId;
                newformula.formula_update_time = DateTime.Now;
           
                //create the details/.
                context.ExecuteStoreCommand("delete from  formula_detail where formulas_id=" + newformula.formulas_id);

                
  
   //             context.ExecuteStoreQuery()

                foreach (var detail in formula.Details)
                {
                    var newtail = new formula_detail();
                    newtail.resource_Id = detail.ResourceId;
                    newtail.formulas_id = formula.FormulaId;
                    newtail.formula_order = detail.Order;
                    newtail.formula_detail_number = detail.Number;
                    newtail.formula_detail_unit = detail.unit;

                    newtail.formula_detail_car = detail.CarNumber;
                    newtail.formula_detail_ischangecar = detail.Ischangecar;


                    context.formula_detail.AddObject(newtail);
                }
                context.SaveChanges();
                return true;
            }
        }

        public static List<FormulaInfo> GetFormulaInfos()
        {
            using (var context = new Entities())
            {
                var res = new List<FormulaInfo>();


                var formulas = context.formulae;
                foreach (var formula in formulas)
                {
                    var newformula = new FormulaInfo();
                    newformula.FormulaId = formula.formulas_id;
                    newformula.FormulaCode = formula.formula_code;
                    newformula.FormulaName = formula.formula_name;
                    
                    newformula.DateCreated = formula.formula_create_time.HasValue?formula.formula_create_time.Value:DateTime.Now;
                    newformula.Create_By =formula.formula_create_by.HasValue?formula.formula_create_by.Value:0;

                    newformula.DateUpdated = formula.formula_update_time.HasValue?formula.formula_update_time.Value:DateTime.Now;
                    newformula.Update_By = formula.formula_update_by.HasValue?formula.formula_update_by.Value:0;

                    newformula.Deactive_By = formula.formula_deactive_by.HasValue?formula.formula_deactive_by.Value:0;
                    newformula.DateDeactive = formula.formula_deactive_time.HasValue?formula.formula_deactive_time.Value:DateTime.Now;

                    newformula.IsActive = formula.formula_isactive.HasValue?formula.formula_isactive.Value:false;
                    newformula.FormulaRemark = formula.formula_remark;



                    newformula.Details = new List<FormulaDetail>();

                    var resources = context.formula_detail.Where(a => a.formulas_id == formula.formulas_id);
                    foreach (var resource in resources)
                    {
                        var newdtail = new FormulaDetail();
                        newdtail.ResourceId = resource.resource_Id.Value;
                        newdtail.Order = resource.formula_order.Value;
                        newdtail.Number = resource.formula_detail_number.HasValue?(decimal)resource.formula_detail_number.Value:0;

                        newdtail.CarNumber = resource.formula_detail_car.HasValue ? resource.formula_detail_car.Value : 1;
                        newdtail.Ischangecar = resource.formula_detail_ischangecar.HasValue ? resource.formula_detail_ischangecar.Value : false;


                        newformula.Details.Add(newdtail);
                    }


                    res.Add(newformula);
                }

                return res;
            }
        }

        //******************************************************************
        //Function: Check the uniqueness of formulaname 
        //Author: wxw
        //Date: 2014/09/05
        public static bool CheckFormulaName(FormulaInfo formulainfo)
        {
            try
            {
                using (var context = new Entities())
                {
                    var findformula = context.formulae.Count(a => a.formula_name == formulainfo.FormulaName);
                    if (findformula == null)
                        return true;
                    else
                        return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }



        public static string GetFormulaInfoById(int formulaId)
        {
            using (var context = new Entities())
            {
                var findformula = context.formulae.FirstOrDefault(a => a.formulas_id==formulaId);
                if (findformula == null)
                {
                    return "未知配方";
                }
                else
                {
                    return findformula.formula_name;
                }
            }

        }
    }
}
